﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;

namespace Logistics
{
    public partial class frmDepartments : Form
    {
        ListViewColumnSorter lvwColumnSorter = new ListViewColumnSorter();
        public frmDepartments()
        {
            InitializeComponent();
            this.listInfo.ListViewItemSorter = lvwColumnSorter;
            ImageList imgList = new ImageList();
            imgList.ImageSize = new Size(1, 20);//分别是宽和高
            listInfo.SmallImageList = imgList;
        }

        #region  编辑
        public void Find()
        {

        }
        public void Add()
        {
            var frm = new frmDepartmentsModify();
            frm.OnModelEvent = AddListviewItem;
            frm.ShowDialog();
        }
        public void Modify()
        {
            if (this.listInfo.SelectedItems.Count == 0)
                return;
            var id = Guid.Parse(this.listInfo.SelectedItems[0].Tag.ToString());
            var objDepartmentInfo = new Manager.tc_Department();
            var model = objDepartmentInfo.GetModel(id);
            var frm = new frmDepartmentsModify(model);
            frm.OnModelEvent = AddListviewItem;
            frm.ShowDialog();
        }
        public void Delete()
        {
            if (this.listInfo.SelectedItems.Count == 0)
                return;
            var item = this.listInfo.SelectedItems[0];
            var msg = "确定要删除'" + item.Text + "'吗？";
            if (MessageBox.Show(msg, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                var id = Guid.Parse(item.Tag.ToString());
                var objDepartmentInfo = new Manager.tc_Department();
                objDepartmentInfo.Delete(id);
                DataBind();
            }

        }

        public void Refreshs()
        {
            DataBind();
        }
        #endregion

        private void AddListviewItem(Model.tc_Department model)
        {
            ListViewItem item = null;
            if (this.listInfo.Items.Count > 0)
            {
                item = MyForms.FindItemWithTag(listInfo, model.Id.ToString());
            }
            if (item == null)
            {
                item = new ListViewItem();
                item.Tag = model.Id.ToString();
                item.Text = model.Department;
                item.SubItems.Add(model.Description);
                this.listInfo.Items.Add(item);
            }
            else
            {
                item.Text = model.Department;
                item.SubItems[1].Text = model.Description;
                if (model.Id != Guid.Empty)
                {
                    if (Guid.Parse(item.Tag.ToString()) == model.Id)
                    {
                        item.Selected = true;
                        item.Focused = true;
                        this.listInfo.EnsureVisible(this.listInfo.Items.IndexOf(item)); // 使该项可见
                        this.listInfo.Focus();
                    }
                }
            }
        }
        public void DataBind()
        {
            this.listInfo.Items.Clear();
            this.listInfo.BeginUpdate();
            var objDepartmentInfo = new Manager.tc_Department();
            var list = objDepartmentInfo.GetList();
            foreach (var item in list)
            {
                AddListviewItem(item);
            }
            this.listInfo.EndUpdate();
        }
        private void listInfo_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == lvwColumnSorter.SortColumn)
            {
                if (lvwColumnSorter.Order == SortOrder.Ascending)
                {
                    lvwColumnSorter.Order = SortOrder.Descending;
                }
                else
                {
                    lvwColumnSorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                lvwColumnSorter.SortColumn = e.Column;
                lvwColumnSorter.Order = SortOrder.Ascending;
            }
            this.listInfo.Sort();
        }

        private void listInfo_DoubleClick(object sender, EventArgs e)
        {
            Modify();
        }

        private void frmDepartmentInfo_Load(object sender, EventArgs e)
        {
            DataBind();
        }

        private void AddMenuItem_Click(object sender, EventArgs e)
        {
            Add();
        }

        private void ModifyMenuItem_Click(object sender, EventArgs e)
        {
            Modify();
        }

        private void DelMenuItem_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void RefreshMenuItem_Click(object sender, EventArgs e)
        {
            Refreshs();
        }
    }
}
